import { baseApiUrl, path } from "../../utils/config";
import { order, orderCancel, orderDelivery } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    tabsList: [],
    status: 0,

    page: 1,
    last_page: 1,
    list: []
  },
  async onLoad(options) {
    let res = await order({
      page: this.data.page,
      limit: 10
    });
    let status = res.status;

    let tabsList = [{ name: "全部订单", id: "" }];
    for (let key in status) {
      tabsList.push({ name: status[key], id: key })
    };
    status = tabsList.length ? tabsList[0].id : "";

    res = res.order;
    let { data, last_page } = res;
    let list = this.data.page == 1 ? data : [...this.data.list, ...data];
    this.setData({ tabsList, status, list, last_page })
  },
  loadmore() {
    let page = this.data.page;
    let last_page = this.data.last_page;
    if (page == last_page) return false;
    page += 1;
    this.setData({ page });
    this.getList();
  },
  async getList() {
    let res = await order({
      stauts: this.data.status,
      page: this.data.page,
      limit: 10
    });
    res = res.order;
    let { data, last_page } = res;
    let list = this.data.page == 1 ? data : [...this.data.list, ...data];
    this.setData({ list, last_page });
  },
  handleTabChange(e) {
    let { current } = e.target.dataset;
    if (this.data.status == current || current === undefined) return;
    this.setData({ page: 1, list: [], status: current });
    this.getList();
  },
  async cancel(e) {
    let { id } = e.currentTarget.dataset;
    await orderCancel({ id });
    Toast.success('操作成功');
    this.setData({ page: 1, list: [] });
    this.getList();
  },
  async delivery(e) {
    let { id } = e.currentTarget.dataset;
    await orderDelivery({ id });
    Toast.success('操作成功');
    this.onLoad();
  },
  router(e) {
    let { id } = e.currentTarget.dataset;
    let url = `/pages/payment/payment?id=${id}`;
    wx.navigateTo({ url });
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})